package com.zy.explore.generator.infrastructure.core;

import com.zy.explore.generator.infrastructure.core.enums.FrameworkTypeEnum;
import com.zy.explore.generator.interfaces.frm.generator.AssemblerFrm;
import com.zy.explore.generator.interfaces.frm.generator.FacadeFrm;
import com.zy.explore.generator.interfaces.frm.generator.FeignFrm;
import com.zy.explore.generator.interfaces.frm.generator.PersistFrm;
import com.zy.explore.generator.interfaces.frm.generator.QueryFrm;
import com.zy.explore.generator.interfaces.frm.generator.SaveFrm;
import com.zy.explore.generator.interfaces.frm.generator.ServiceFrm;
import com.zy.explore.generator.interfaces.frm.generator.ValidatorFrm;
import com.zy.explore.generator.interfaces.frm.generator.VoFrm;

import java.util.Objects;
import java.util.Properties;

/**
 * 建类说明：生成框架代码
 * 当前版本：1.0.0
 *
 * @author 中国印
 * @date 2022年01月09日 23:04:19
 * @since 1.0.0
 */
public class GenerateFramework {

    /**
     * 属性说明：java src
     *
     * @see String
     */
    private static final String JAVA_SRC = "/src/main/java/";
    /**
     * 属性说明：资源src
     *
     * @see String
     */
    private static final String RESOURCES_SRC = "/src/main/resources/";

    /**
     * 方法说明：生成Feign
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generateFeign(Properties config, FeignFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        int frameworkType = Integer.parseInt(config.getProperty("frameworkType"));
        if (Objects.equals(frameworkType, FrameworkTypeEnum.DDD_FEIGN.getType())) {
            String targetPath = rootPath + "/"
                    + projectName
                    + JAVA_SRC
                    + frm.getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("feignPath") + "/"
                    + config.getProperty("persistFramework")
                    + tableClassNamePrefix
                    + "Feign.java";
            String tplName = config.getProperty("feignTplName");
            String tplPath = RESOURCES_SRC + config.getProperty("tplPath") + "/";
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成JpaFeign文件成功");
        }
    }

    /**
     * 方法说明：生成Facade
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generateFacade(Properties config, FacadeFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        String targetPath = rootPath
                + projectName
                + JAVA_SRC + "/"
                + frm.getBasePackageName().replace(".", "/") + "/"
                + config.getProperty("facadePath") + "/"
                + config.getProperty("persistFramework")
                + tableClassNamePrefix
                + "Facade.java";
        String tplName = config.getProperty("facadeTplName");
        String tplPath = RESOURCES_SRC + config.getProperty("jpaTplPath");
        GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
        System.out.println("生成JpaFacade文件成功");
    }

    /**
     * 方法说明：生成Assembler
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generateAssembler(Properties config, AssemblerFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        String targetPath = rootPath
                + projectName
                + JAVA_SRC + "/"
                + frm.getBasePackageName().replace(".", "/") + "/"
                + config.getProperty("assemblerPath") + "/"
                + tableClassNamePrefix
                + "JpaAssembler.java";
        String tplName = config.getProperty("jpaAssemblerTplName");
        String tplPath = RESOURCES_SRC + config.getProperty("jpaTplPath");
        GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
        System.out.println("生成JpaAssembler文件成功");
    }

    /**
     * 方法说明：生成Validator
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generateValidator(Properties config, ValidatorFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        String targetPath = rootPath
                + projectName
                + JAVA_SRC + "/"
                + frm.getBasePackageName().replace(".", "/") + "/"
                + config.getProperty("validatorPath") + "/"
                + tableClassNamePrefix
                + "JpaValidator.java";
        String tplName = config.getProperty("jpaValidatorTplName");
        String tplPath = RESOURCES_SRC + config.getProperty("jpaTplPath");
        GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
        System.out.println("生成JpaAssembler文件成功");
    }

    /**
     * 方法说明：生成Service
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generateService(Properties config, ServiceFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        String targetPath = rootPath
                + projectName
                + JAVA_SRC
                + frm.getBasePackageName().replace(".", "/") + "/"
                + config.getProperty("servicePath") + "/"
                + tableClassNamePrefix
                + "JpaService.java";
        String tplName = config.getProperty("jpaServiceTplName");
        String tplPath = RESOURCES_SRC + config.getProperty("jpaTplPath");
        GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
        System.out.println("生成JpaAssembler文件成功");
    }

    /**
     * 方法说明：生成Assembler
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generatePersistLayer(Properties config, PersistFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        if (frm.getUseJpa()) {
            //region 1.生成Entity类
            String targetPath = rootPath
                    + projectName
                    + JAVA_SRC
                    + frm.getEntityFrm().getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("entityPath") + "/Jpa"
                    + tableClassNamePrefix
                    + ".java";
            String tplName = config.getProperty("jpaEntityTplName");
            String tplPath = RESOURCES_SRC + config.getProperty("jpaTplPath");
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成JpaEntity文件成功");
            //endregion

            //region 2.生成Repository类
            targetPath = rootPath
                    + projectName
                    + "/src/main/java/"
                    + frm.getRepositoryFrm().getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("repositoryPath") + "/Jpa"
                    + tableClassNamePrefix
                    + "Repository.java";
            tplName = config.getProperty("jpaRepositoryTplName");
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成JpaRepository文件成功");
            //endregion
        }
        if (frm.getUseTkMybatis()) {
            //region 1.生成Entity类
            String targetPath = rootPath
                    + projectName
                    + JAVA_SRC
                    + frm.getEntityFrm().getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("entityPath") + "/Tk"
                    + tableClassNamePrefix
                    + ".java";
            String tplName = config.getProperty("tkEntityTplName");
            String tplPath = RESOURCES_SRC + config.getProperty("tkMybatisTplPath");
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成TkMybatisEntity文件成功");
            //endregion

            //region 2.生成Repository类
            targetPath = rootPath
                    + projectName
                    + JAVA_SRC
                    + frm.getRepositoryFrm().getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("repositoryPath") + "/Tk"
                    + tableClassNamePrefix
                    + "Dao.java";
            tplName = config.getProperty("tkRepositoryTplName");
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成TkMybatisRepository文件成功");
            //endregion

            //region 3.生成Xml文件
            targetPath = rootPath
                    + projectName
                    + JAVA_SRC
                    + frm.getXmlFrm().getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("xmlMapperPath") + "/Tk"
                    + tableClassNamePrefix
                    + "Dao.xml";
            tplName = config.getProperty("xmlTkTplName");
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成TkMybatisDaoXml文件成功");
            //endregion
        }
        if (frm.getUseMybatisPlus()) {
            //region 1.生成Entity类
            String targetPath = rootPath
                    + projectName
                    + JAVA_SRC
                    + frm.getEntityFrm().getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("entityPath") + "/Mp"
                    + tableClassNamePrefix
                    + ".java";
            String tplName = config.getProperty("mpEntityTplName");
            String tplPath = RESOURCES_SRC + config.getProperty("mybatisPlusTplPath");
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成MpMybatisEntity文件成功");
            //endregion

            //region 2.生成Repository类
            targetPath = rootPath
                    + projectName
                    + JAVA_SRC
                    + frm.getRepositoryFrm().getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("repositoryPath") + "/Mp"
                    + tableClassNamePrefix
                    + "Dao.java";
            tplName = config.getProperty("mpRepositoryTplName");
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成MpMybatisRepository文件成功");
            //endregion

            //region 3.生成Xml文件
            targetPath = rootPath
                    + projectName
                    + JAVA_SRC
                    + frm.getXmlFrm().getBasePackageName().replace(".", "/") + "/"
                    + config.getProperty("xmlMapperPath") + "/Mp"
                    + tableClassNamePrefix
                    + "Dao.xml";
            tplName = config.getProperty("xmlMpTplName");
            GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
            System.out.println("生成MpMybatisDaoXml文件成功");
            //endregion
        }
    }

    /**
     * 方法说明：生成QueryFrm
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generateQueryFrm(Properties config, QueryFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        String targetPath = rootPath
                + projectName
                + JAVA_SRC
                + frm.getBasePackageName().replace(".", "/") + "/"
                + config.getProperty("frmPath") + "/"
                + tableClassNamePrefix
                + "QueryFrm.java";
        String tplName = config.getProperty("queryFrmTplName");
        String tplPath = RESOURCES_SRC + config.getProperty("commonTplPath");
        GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
        System.out.println("生成QueryFrm文件成功");
    }

    /**
     * 方法说明：生成SaveFrm
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generateSaveFrm(Properties config, SaveFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        String targetPath = rootPath
                + projectName
                + JAVA_SRC
                + frm.getBasePackageName().replace(".", "/") + "/"
                + config.getProperty("frmPath") + "/"
                + tableClassNamePrefix
                + "SaveFrm.java";
        String tplName = config.getProperty("saveFrmTplName");
        String tplPath = RESOURCES_SRC + config.getProperty("commonTplPath");
        GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
        System.out.println("生成JpaAssembler文件成功");
    }

    /**
     * 方法说明：生成VO
     *
     * @param config               配置信息
     * @param frm                  模板参数对象
     * @param rootPath             生成文件的根路径
     * @param projectName          生成文件的项目名称
     * @param tableClassNamePrefix 表对应类名称前缀
     * @throws Exception 异常
     */
    public static void generateVo(Properties config, VoFrm frm, String rootPath, String projectName, String tableClassNamePrefix) throws Exception {
        String targetPath = rootPath
                + projectName
                + JAVA_SRC
                + frm.getBasePackageName().replace(".", "/") + "/"
                + config.getProperty("voPath") + "/"
                + tableClassNamePrefix
                + "VO.java";
        String tplName = config.getProperty("voTplName");
        String tplPath = RESOURCES_SRC + config.getProperty("commonTplPath");
        GenerateCore.doGenerateFile(frm, tplName, tplPath, targetPath);
        System.out.println("生成JpaAssembler文件成功");
    }


}
